﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using System.ServiceModel;
using System.Data.SqlClient;
using System.Collections;
using TrainingWorkshop.MVPWebsite.Role.Views;
using TrainingWorkshop.MVPWebsite.Role.Interface;
using System.Windows.Forms;
using TrainingWorkshop.MVPWebsite.Authority.Views;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
	public partial class RoleDetail : Microsoft.Practices.CompositeWeb.Web.UI.Page, IRoleDetailView
	{
        private RoleDetailPresenter _presenter;
        
        #region Property

        public enum RoleStatus
        {
            InUse = 1,
            NoUse = 2
        }
        private DataContracts.RoleDTO _roleDTO;
        public DataContracts.RoleDTO roleDTO
        {
            get
            {
                return _roleDTO;
            }
            set
            {
                _roleDTO = value;

            }
        }

        public List<AuthorityDTO> DisplayAllAuthority
        {
            set
            {
                this.CheckBoxList1.DataSource = value;
                this.CheckBoxList1.DataTextField = "AuthorityName";
                this.CheckBoxList1.DataValueField = "AuthorityId";
                this.CheckBoxList1.DataBind();
            }
        }

        public StatusDTO SelectedStatus
        {
            set
            {
                if (value.StatusId == 1)
                {

                    RadioButtonList1.Items[0].Selected = true;
                }
                if (value.StatusId == 2)
                {

                    RadioButtonList1.Items[1].Selected = true;
                }
            }
        }
        public string DisplayRoleName
        {
            set
            {
                RoleName.Text = roleDTO.RoleName;
            }
        }

        public List<AuthorityDTO> SelectedAuthority
        {
            set
            {
                int authority_count = CheckBoxList1.Items.Count;

                ArrayList authorityaalllist = new ArrayList();
                for (int i = 0; i < roleDTO.Authorities.Count; i++)
                {
                    authorityaalllist.Add(roleDTO.Authorities.ElementAt(i).AuthorityId.ToString());
                }

                for (int q = 0; q < CheckBoxList1.Items.Count; q++)
                {
                    if (authorityaalllist.IndexOf(CheckBoxList1.Items[q].Value) != -1)
                    { CheckBoxList1.Items[q].Selected = true; }
                }
            }
        }

        public int GetCheckboxlistCount
        {
            get
            {
                int CheckBoxlistcount = CheckBoxList1.Items.Count;
                return CheckBoxlistcount;
                
            }
         
        }

  
        public int GetTransRoleId
        {
            get
            {
                string roleid_temp = Request.QueryString["roleid_trans"];
                int role_Id = Convert.ToInt32(roleid_temp);
               // int role_Id = 43;
                return role_Id;
            }
        }

        public string GetRoleName
        {
            get
            {
                RoleDTO roleDTO = new RoleDTO();
                return roleDTO.RoleName = RoleName.Text.ToString().Trim();
            }
        }

        public StatusDTO SelectStatus
        {
            get
            {
                roleDTO.Status = new DataContracts.StatusDTO();

                if (RadioButtonList1.Items[0].Selected)
                {
                    roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.InUse);
                }

                if (RadioButtonList1.Items[1].Selected)
                {
                    roleDTO.Status.StatusId = Convert.ToInt32(RoleStatus.NoUse);
                }
                return roleDTO.Status;
            }

        }

        public List<AuthorityDTO> UpdateRoleAuthority
        {
            get
            {
                roleDTO.Authorities = new List<AuthorityDTO>();
                roleDTO.Authorities.Clear();
                int count = CheckBoxList1.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        AuthorityDTO authority = new AuthorityDTO();
                        authority.AuthorityName = CheckBoxList1.Items[i].Text;
                        authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                        roleDTO.Authorities.Add(authority);
                    }
                }
                return roleDTO.Authorities;
            }
        }

        public List<AuthorityDTO> AddRoleAuthority
        {
            get
            {
                roleDTO.Authorities = new List<AuthorityDTO>();
                int count = CheckBoxList1.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        AuthorityDTO authority = new AuthorityDTO();
                        authority.AuthorityName = CheckBoxList1.Items[i].Text;
                        authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                        roleDTO.Authorities.Add(authority);
                    }
                }
                return roleDTO.Authorities;
            }
        }

        public void ShowOperationMessage(string message)
        {
            
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }

        public void Redirect(string url)
        {
            Response.Redirect(url);
        }
       
        #endregion


        [CreateNew]
        public RoleDetailPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

               
                this._presenter.DisplayRoleDetail();
                
            }
        }

        protected void saverole_Click(object sender, EventArgs e)
        {
           
            this._presenter.AddOrUpdateRoleDetail();
        }

  
	}
}

